package org.nanowrimo.ws.test;

import junit.framework.TestCase;
import org.nanowrimo.ws.NanowrimoWebServiceAPI;
import org.nanowrimo.ws.domain.RegionSummary;
import org.nanowrimo.ws.domain.UserSummary;
import org.nanowrimo.ws.domain.WordCountListEntry;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.SimpleDateFormat;

public class NanowrimoWebServiceAPI_UT extends TestCase {
  public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
  private MockServiceAPI api;
  private String inputXML;

  public NanowrimoWebServiceAPI_UT(String name) {
    super(name);
  }

  protected void setUp() throws Exception {
    api = new MockServiceAPI();
  }

  public void testUserSummary() throws Exception {
    inputXML = "<wc>\n" +
      "<uid>30837</uid>\n" +
      "<uname>NewMexicoKid</uname>\n" +
      "<user_wordcount>2699</user_wordcount>\n" +
      "</wc>";

    UserSummary summary = (UserSummary) api.getUserSummary(0);

    assertEquals(30837, summary.getUserID());
    assertEquals("NewMexicoKid", summary.getName());
    assertEquals(2699, summary.getWordcount());

    WordCountListEntry[] entries = summary.getEntries();
    assertNull(entries);
  }

  public void testUserHistory() throws Exception {
    inputXML = "<wchistory>\n" +
      "<uid>30837</uid>\n" +
      "<uname>NewMexicoKid</uname>\n" +
      "<user_wordcount>25001</user_wordcount>\n" +
      "<wordcounts>\n" +
      "<wcentry><wc>2699</wc><wcdate>2005-11-01</wcdate></wcentry>\n" +
      "<wcentry><wc>2315</wc><wcdate>2005-11-02</wcdate></wcentry>\n" +
      "<wcentry><wc>6302</wc><wcdate>2005-11-04</wcdate></wcentry>\n" +
      "</wordcounts>\n" +
      "</wchistory>";

    UserSummary summary = api.getUserHistory(0);

    assertEquals(30837, summary.getUserID());
    assertEquals("NewMexicoKid", summary.getName());
    assertEquals(25001, summary.getWordcount());

    WordCountListEntry[] entries = summary.getEntries();
    assertNotNull(entries);
    assertEquals(3, entries.length);
    assertEquals(2699, entries[0].getWordcount());
    assertEquals(2315, entries[1].getWordcount());
    assertEquals(6302, entries[2].getWordcount());

    assertEquals("2005-11-01", SDF.format(entries[0].getDate()));
    assertEquals("2005-11-02", SDF.format(entries[1].getDate()));
    assertEquals("2005-11-04", SDF.format(entries[2].getDate()));
  }

  public void testRegionSummary() throws Exception {
    inputXML = "<wc>\n" +
      "<rid>3</rid>\n" +
      "<rname>Arizona :: Elsewhere</rname>\n" +
      "<region_wordcount>25320</region_wordcount>\n" +
      "</wc>";

    RegionSummary region = (RegionSummary) api.getRegionSummary(0);

    assertEquals(3, region.getRegionID());
    assertEquals("Arizona :: Elsewhere", region.getName());
    assertEquals(0, region.getRank());
    assertEquals(25320, region.getWordcount());

    WordCountListEntry[] entries = region.getEntries();
    assertNull(entries);
  }

  public void testRegionHistory() throws Exception {
    inputXML = "<wcregionhist>\n" +
      "<rid>3</rid>\n" +
      "<rname>Arizona :: Elsewhere</rname>\n" +
      "<wcrank>135</wcrank>\n" +
      "<wordcounts>\n" +
      "<wcentry><wc>11320</wc><wcdate>2005-11-01</wcdate></wcentry>\n" +
      "<wcentry><wc>13727</wc><wcdate>2005-11-02</wcdate></wcentry>\n" +
      "<wcentry><wc>14288</wc><wcdate>2005-11-03</wcdate></wcentry>\n" +
      "<wcentry><wc>14288</wc><wcdate>2005-11-04</wcdate></wcentry>\n" +
      "<wcentry><wc>25320</wc><wcdate>2005-11-05</wcdate></wcentry>\n" +
      "</wordcounts>\n" +
      "</wcregionhist>";

    RegionSummary region = api.getRegionHistory(0);

    assertEquals(3, region.getRegionID());
    assertEquals("Arizona :: Elsewhere", region.getName());
    assertEquals(135, region.getRank());
    assertEquals(0, region.getWordcount());

    WordCountListEntry[] entries = region.getEntries();
    assertNotNull(entries);

    assertEquals(5, entries.length);
    assertEquals(11320, entries[0].getWordcount());
    assertEquals(13727, entries[1].getWordcount());
    assertEquals(14288, entries[2].getWordcount());
    assertEquals(14288, entries[3].getWordcount());
    assertEquals(25320, entries[4].getWordcount());

    assertEquals("2005-11-01", SDF.format(entries[0].getDate()));
    assertEquals("2005-11-02", SDF.format(entries[1].getDate()));
    assertEquals("2005-11-03", SDF.format(entries[2].getDate()));
    assertEquals("2005-11-04", SDF.format(entries[3].getDate()));
    assertEquals("2005-11-05", SDF.format(entries[4].getDate()));
  }

  private class MockServiceAPI extends NanowrimoWebServiceAPI {
    protected Reader getServerStreamReader(String urlString) throws IOException {
      return new StringReader(inputXML);
    }
  }
}
